package c17网络编程;

import java.io.*;
import java.net.*;

public class TestUrl {
	public static void main(String[] args) throws IOException {
		URL url = new URL("http://www.baidu.com/index.php?tn=98012088_3_dg");

		System.err.println("Protocol = " + url.getProtocol());
		System.err.println("Host = " + url.getHost());
		System.err.println("Port = " + url.getPort());
		System.err.println("File = " + url.getFile());
		System.err.println("Path = " + url.getPath());
		System.err.println("Query = " + url.getQuery());
		//获取此 URL 的授权部分
		System.err.println("Authority = " + url.getAuthority());
		//获取与此 URL 关联协议的默认端口号
		System.err.println("Default port = " + url.getDefaultPort());
		//锚点（也称为“引用”）
		System.err.println("Ref = " + url.getRef());
		System.err.println("User Info = " + url.getUserInfo());

		System.err.println(url.toExternalForm());
		//获取此 URL 的内容。此方法是下面方法的缩写：openConnection().getContent()
		System.err.println(url.getContent());

		//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream
		InputStreamReader isr = new InputStreamReader(url.openStream());
		BufferedReader br = new BufferedReader(isr);
		BufferedWriter bw = new BufferedWriter(new FileWriter("baidu.html"));
		String s;
		while ((s = br.readLine()) != null) {
			System.err.print(s);
			bw.write(s);
		}
		bw.close();
		br.close();
	}
}